package com.smooth.mall.controller;

import com.github.pagehelper.PageInfo;
import com.smooth.mall.common.ApiRestResponse;
import com.smooth.mall.model.vo.OrderVO;
import com.smooth.mall.request.CreateOrderReq;
import com.smooth.mall.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("创建订单请求")
    @PostMapping("/order/create")
    public ApiRestResponse createOrder(@RequestBody @Valid CreateOrderReq createOrderReq){
        String orderNumber = orderService.create(createOrderReq);
        return  ApiRestResponse.success(orderNumber);
    }

    @ApiOperation("订单详情请求")
    @GetMapping("/order/detail")
    public ApiRestResponse detail(@RequestParam("orderNo") String orderNo) {
        OrderVO orderVO = orderService.detail(orderNo);
        return ApiRestResponse.success(orderVO);
    }

    @ApiOperation("前台订单列表请求")
    @GetMapping("/order/list")
    public ApiRestResponse list(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        PageInfo pageInfo = orderService.listForCustomer(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("取消订单请求")
    @PostMapping("/order/cancel")
    public ApiRestResponse cancel(@RequestParam("orderNo") String orderNo){
        orderService.cancel(orderNo);
        return ApiRestResponse.success();
    }

    @ApiOperation("生成支付二维码请求")
    @PostMapping("/order/qrcode")
    public ApiRestResponse qrcode(@RequestParam("orderNo") String orderNo) {
        String pngAddress = orderService.qrcode(orderNo);
        return ApiRestResponse.success(pngAddress);
    }

    @ApiOperation("支付订单请求")
    @GetMapping("/pay")
    public ApiRestResponse pay(@RequestParam("orderNo") String orderNo) {
        orderService.pay(orderNo);
        return ApiRestResponse.success();
    }
}
